<!DOCTYPE html>
<html lang="zh-CN">
  <head>
    <meta charset="utf-8" />
    <meta
      name="viewport"
      content="width=device-width, initial-scale=1,maximum-scale=1,user-scalable=no"
    />
    <meta name="apple-mobile-web-app-capable" content="yes" />
    <title>news list</title>
    <link rel="stylesheet" href="./flowjs/css/minirefresh.css" />
    <link rel="stylesheet" href="./flowjs/css/home.css" />

    <!-- <script async src="https://pagead2.googlesyndication.com/pagead/js/adsbygoogle.js?client=ca-pub-7725717395742252"
    crossorigin="anonymous"></script> -->

    <script
      async
      src="https://securepubads.g.doubleclick.net/tag/js/gpt.js"
    ></script>

    <!-- <script src="https://jsc.mgid.com/site/998641.js" async>
  </script>


  <script src="https://jsc.mgid.com/site/998642.js" async>
  </script> -->

    <script src="./flowjs/utils/index.js"></script>
    <script src="./flowjs/utils/http.js"></script>
  </head>

  <body>
    <div id="app">
      <div class="content">
        <div id="minirefresh" class="minirefresh-wrap">
          <div class="minirefresh-scroll">
            <div class="container">
              <div class="news-list" id="listdata"></div>
            </div>
          </div>
        </div>
      </div>
    </div>
    <script>
      // 换广告代码
      // var domains = ["en-in-feeds.obnews.co", "hi-feeds.obnews.co"];
      // var hostname = window.location.hostname || document.domain;
      // if (domains.includes(hostname)) {
      //   loadScript02(
      //     "https://securepubads.g.doubleclick.net/tag/js/gpt.js",
      //     function () {
      //       console.log("Script loaded!");
      //     }
      //   );
      // } else {
      //   loadScript01(
      //     "https://pagead2.googlesyndication.com/pagead/js/adsbygoogle.js?client=ca-pub-7725717395742252",
      //     function () {
      //       console.log("Script loaded!");
      //     }
      //   );
      // }

      function loadScript02(url, callback, crossorigin = false) {
        try {
          var script = document.createElement("script");
          script.type = "text/javascript";
          script.async = true;
          crossorigin && script.setAttribute("crossorigin", "anonymous");
          if (script.readyState) {
            script.onreadystatechange = function () {
              if (
                script.readyState == "loaded" ||
                script.readyState == "complete"
              ) {
                script.onreadystatechange = null;
                callback && callback();
              }
            };
          } else {
            script.onload = function () {
              callback && callback();
            };
          }
          script.src = url;
          document.head.appendChild(script);
        } catch (e) {
          console.error("Error loading script:", e);
        }
      }
      if (window.location.hostname === "en-in-feeds.obnews.co") {
        loadScript02("https://jsc.mgid.com/site/998641.js", function () {
          console.log("Script loaded!");
        });
      } else if (window.location.hostname === "id-feeds.mobicrazy.com") {
        loadScript02("https://jsc.mgid.com/site/998642.js", function () {
          console.log("Script loaded!");
        });
      } else if (window.location.hostname === "th-feeds.mobicrazy.com") {
        loadScript02("https://jsc.mgid.com/site/998643.js", function () {
          console.log("Script loaded!");
        });
      } else if (window.location.hostname === "hi-feeds.mobicrazy.com") {
        loadScript02("https://jsc.mgid.com/site/1002485.js", function () {
          console.log("Script loaded!");
        });
      } else {
        loadScript02(
          "https://pagead2.googlesyndication.com/pagead/js/adsbygoogle.js?client=ca-pub-7725717395742252",
          function () {
            console.log("Script loaded!");
          },
          true
        );
      }
    </script>
    <script src="./flowjs/js/initApp.js"></script>
    <script src="./flowjs/js/config.js"></script>
    <script src="./flowjs/js/minirefresh.js"></script>

    <script src="./flowjs/js/home.js"></script>
    <script src="./flowjs/js/compileTpl.js"></script>

    <!-- 下拉刷新，上拉加载 -->
    <script>
      var container = document.querySelector(".container");
      if (
        ["en-in-feeds.mobicrazy.com", "en-in-feeds.obnews.co"].includes(
          document.domain
        )
      ) {
        container.classList.add("en");
      } else {
        container.classList.remove("en");
      }
      let dataList = [];
      localStorage.setItem("ids", JSON.stringify([]));

      var appendTestData = CompileTpl.appendTestData,
        bindEvent = CompileTpl.bindEvent,
        pageSize = 10,
        listDom = document.querySelector("#listdata"),
        requestDelayTime = 600;
      var miniRefresh = new MiniRefresh({
        container: "#minirefresh",
        down: {
          isLock: true,
          callback: function () {
            fetchHomeData()
              .then((resData) => {
                dataList = (resData?.data?.rows ?? []).map((item, index) => {
                  item.pubtime = formatTime(item.pubtime);
                  item.exposure_times ??= "0";
                  item.d_key ??= fullOpen(0, 10 ** 10) + "";
                  return item;
                });

                appendTestData(listDom, dataList, true);
                tracking();
                miniRefresh.endDownLoading(true);
              })
              .catch((err) => {
                miniRefresh.endDownLoading(true);
              });
          },
        },
        up: {
          isAuto: true,
          isLock: false,
          callback: async function () {
            try {
              var resData = await fetchHomeData();
              var rows = resData?.data?.rows ?? [];
              dataList = [...dataList, ...rows].map((item, index) => {
                item.pubtime = formatTime(item.pubtime);
                item.exposure_times ??= "0";
                item.d_key ??= fullOpen(0, 10 ** 10) + "";
                return item;
              });
              appendTestData(listDom, dataList, false);
              tracking();
              // miniRefresh.endUpLoading(rows.length < pageSize ? true : false); // 接口返回数据异常：传limit:10 返回7条数据
              miniRefresh.endUpLoading(isEmpty(rows) ? true : false);
            } catch (error) {
              miniRefresh.endUpLoading(true);
            }
          },
        },
      });
    </script>
    <!-- 统计页面的UV（Unique Visitor，独立访客）和PV（Page View，页面浏览量） -->
    <script>
      var parent = document.querySelector("#listdata");
      function isInViewport(element, index) {
        var observer = new IntersectionObserver(
          ([{ target, isIntersecting }]) => {
            if (isIntersecting) {
              let elIndex = target.getAttribute("index") * 1;
              let exposure_times = target.getAttribute("exposure_times");
              target.setAttribute("exposure_times", ++exposure_times);
              var id = target.id;
              var item = dataList.find((item) => +item.d_key === +id);
              item.exposure_times = exposure_times + "";
              if (+exposure_times === 1 && !!item.impression_tracking_url) {
                var test = getSearchParam("test");
                var url =
                  item.impression_tracking_url +
                  (!!test ? "&test=" + test : "");
                sendTrack(url);
              }
              if (elIndex % 3 === 0 && elIndex != 0) {
                insertAfterAds(parent, target, elIndex);
              }
              if (isNaturalNumbers((elIndex - 1) / 3 - 1)) {
                insertBeforeAds(parent, target, elIndex);
              }
              observer.unobserve(element);
            }
          },
          {
            root: null,
            rootMargin: "0px",
            threshold: 0.5,
          }
        );
        // observer.unobserve(element);
        observer.observe(element);
      }
      function tracking() {
        var elements = document.querySelectorAll(".list-item");
        elements.forEach((element, index) => {
          isInViewport(element, index);
        });
      }
    </script>
  </body>
</html>
